feat(yoco): implement YOCO fast-prefill branch in Gemma4TextModel (PR-B/3) - #12
Draft
pyc96 wants to merge 1 commit into
Draft
feat(yoco): implement YOCO fast-prefill branch in Gemma4TextModel (PR-B/3)#12pyc96 wants to merge 1 commit into
pyc96 wants to merge 1 commit into
Conversation
…-B/3)
When --kv-sharing-fast-prefill is on AND the served Gemma-4 checkpoint
has num_kv_shared_layers > 0 (currently Gemma-4 E4B-it and E2B-it, plus
any future Gemma-4 variant that uses KV-sharing), the EXTEND-mode layer
loop now splits into a self-decoder + a cross-decoder, where the cross-
decoder runs only on the per-request last-extend-token rows. The cross-
decoder output is scattered back into a full-shape hidden_states tensor
so the LM head, frozen-KV MTP worker, and any other downstream consumer
sees the existing [T, H] contract.
Note: gemma-4-26b-a4b-it and gemma-4-31B-it both have num_kv_shared_layers
= 0 in their HF text_config, so YOCO does NOT fire for those checkpoints
(the predicate returns False). The target benchmark model for this stack
is google/gemma-4-E4B-it (num_kv_shared_layers=18, num_hidden_layers=42,
PLE enabled hidden_size_per_layer_input=256). PR-C runs the bench there.
Mechanism mirrors vllm/model_executor/models/gemma4.py:1190-1273
(fast_prefill_forward), reimplemented in SGLang's own forward + attention
metadata abstractions:
* Gemma4TextModel.__init__: caches first_kv_shared_layer_idx and reads the
flag from get_global_server_args().
* Gemma4TextModel._can_run_yoco(forward_batch): predicate that gates the
branch. Returns False unless flag-on AND model has KV-shared layers AND
forward_mode is EXTEND-not-TARGET_VERIFY AND extend_seq_lens populated
AND no input-token logprobs AND no Eagle3 aux-hidden capture AND the
back-half boundary actually falls inside the local PP rank's layer
range. PLE-enabled variants (E4B/E2B) are supported via the per-layer
inputs gather below.
* Gemma4TextModel._build_cross_decoder_last_token_index(forward_batch):
per-request last-extend-token row index. Matches
LogitsProcessor._get_pruned_states cumsum-1 (or the padded-static-len
variant for piecewise CUDA graph).
* Gemma4TextModel._run_cross_decoder_with_yoco(...): temporarily mutates
forward_batch.forward_mode to DECODE so the triton attention backend
rebuilds its metadata as qo_indptr=[0..B], max_extend_len=1,
kv_indptr=cumsum(seq_lens), kv_indices over the full prefix+extend KV
span per request (from the donor layer's KV pool). Runs the KV-shared
layers on the gathered Q rows, then restores mode and metadata in a
try/finally so the caller sees no externally visible mutation. Also
gathers per_layer_inputs to the same rows when PLE is enabled.
* Gemma4TextModel.forward: when _can_run_yoco fires, runs layers
[start_layer, first_kv_shared_layer_idx) on the full extend-token
batch, calls _run_cross_decoder_with_yoco, then scatters via
hidden_states.clone().index_copy_(0, last_token_index, cross_hidden)
so the final norm sees a full-shape tensor.
Tests: test/registered/unit/models/test_gemma4_yoco.py adds 16 unit
tests covering:
* TestCanRunYoco: 11 cases (eligible, flag-off, no-KV-shared, decode,
target-verify, no-extend-lens, PLE-enabled-accepted, input-logprobs,
eagle3-capture, PP-no-back-half, PP-no-front-half)
* TestBuildCrossDecoderLastTokenIndex: 2 cases (non-padded cumsum-1,
padded-static-len)
* TestRunCrossDecoderWithYoco: 3 cases (mode+metadata restore,
per_layer_inputs gather, exception-still-restores)
All 16 tests pass:
Ran 16 tests in 0.004s
OK
Diff in gemma4_causal.py is +188/-5 (modest reflow noise from auto-format).
Stack base: pyc/yoco-fast-prefill-config @ 0911a96 (PR-A: flag plumbing)
Next: PR-C/3 (E4B benchmark + parity test + tuning).
Plan: .humanize/yoco-gemma4/refined-plan.md
Co-authored-by: Claude
This was referenced May 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on PR-A (#11). Adds the model-side YOCO fast-prefill branch to `Gemma4TextModel.forward` so that, when `--kv-sharing-fast-prefill` is on AND the served checkpoint has `num_kv_shared_layers > 0`, the last N decoder layers run only on the per-request last-extend-token rows during EXTEND-mode forwards.
Mirrors vLLM's `fast_prefill_forward` (`vllm/model_executor/models/gemma4.py:1190-1273`), reimplemented in SGLang's own forward + attention-metadata abstractions.
Target model
The Gemma-4 family checkpoints have:
PR-C benches against google/gemma-4-E4B-it (4.5B params, ~9 GB bf16, 42 layers split 24 self + 18 cross).
What's in PR-B
Mechanism
Tests
```
$ python test/registered/unit/models/test_gemma4_yoco.py
................
Ran 16 tests in 0.004s
OK
```
Predicate (AC-2)
`_can_run_yoco` returns True only when all of:
KV cache integrity (AC-5)
Benchmark / MMLU
No runtime change for any currently-benchmarked target (26b-a4b-it and 31B-it have num_kv_shared_layers=0 so the predicate returns False and the existing layer loop runs verbatim). E4B-it bench results land in PR-C.
Stack
Stack base: `pyc/yoco-fast-prefill-config` @ `0911a9627`
Plan: `.humanize/yoco-gemma4/refined-plan.md`
CI States
Latest PR Test (Base): ❌ Missing
run-cilabel -- add it to run CI tests.Latest PR Test (Extra): ❌ Blocked --
run-ciis required first.